﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.orderLinqDAL;
using dhSystemBLL.orderData;
using dhsystem.produceForm;
using baseSet.importExport;
using dhDataAcess.baseBLL;

namespace dhsystem.orderForm
{
    public partial class FrmscrlDetailAdd : Form
    {
        scrlBLL scrl = new scrlBLL();
        scrlDetailModel scrlDetailRow = new scrlDetailModel();
        materialListBLL material = new materialListBLL();
        BindingSource BindingSourcescrlDetailAdd;
        productUnitBLL productunit = new productUnitBLL();
        string state;
        string billNo;
        //绑定数据源
        public FrmscrlDetailAdd(BindingSource bindingSourcescrlDetail, string _state, string _billNo)
        {
            InitializeComponent();
            BindingSourcescrlDetailAdd = bindingSourcescrlDetail;
            state = _state;
            billNo = _billNo;
        }

        private void textPz_TextChanged(object sender, EventArgs e)
        {

        }
        //打开窗口时绑定数据
        private void FrmscrlDetailAdd_Load(object sender, EventArgs e)
        {
            //bindingSourceunit.DataSource = productunit.getproductUnitAll ();
            //comboBoxunit.DataSource = bindingSourceunit;
            //comboBoxunit.DisplayMember = "unitEn";
            //comboBoxunit.ValueMember = "unitEn";
            //comboBoxunit.SelectedValue = "Kg";
            //bindingSourcematerial.DataSource = material.getmaterialListAll();
            //comboBoxmatNum.DataSource = bindingSourcematerial;
            //comboBoxmatNum.DisplayMember = "matName";
            //comboBoxmatNum.ValueMember = "matNum";

            if (state == "编辑")
            {
                textbillNo.DataBindings.Add("text", BindingSourcescrlDetailAdd, "billNo");
                textmatNum.DataBindings.Add("text", BindingSourcescrlDetailAdd, "matNum");
                textmatName.DataBindings.Add("text", BindingSourcescrlDetailAdd, "matName");
                textmatSpec.DataBindings.Add("text", BindingSourcescrlDetailAdd, "matSpec");
                textqty.DataBindings.Add("text", BindingSourcescrlDetailAdd, "qty");
                textunit.DataBindings.Add("text", BindingSourcescrlDetailAdd, "unit");
                textmark.DataBindings.Add("text", BindingSourcescrlDetailAdd, "mark");
                //textid.DataBindings.Add("text", BindingSourcescrlDetailAdd, "recordid");
                // BindingSourceDetailAdd.AddNew();
                //textproductname.Text = productname;
            }
            if (state == "新增")
            {
                textbillNo.DataBindings.Add("text", BindingSourcescrlDetailAdd, "billNo");
                textmatNum.DataBindings.Add("text", BindingSourcescrlDetailAdd, "matNum");
                textmatName.DataBindings.Add("text", BindingSourcescrlDetailAdd, "matName");
                textmatSpec.DataBindings.Add("text", BindingSourcescrlDetailAdd, "matSpec");
                textqty.DataBindings.Add("text", BindingSourcescrlDetailAdd, "qty");
                textunit.DataBindings.Add("text", BindingSourcescrlDetailAdd, "unit");
                textmark.DataBindings.Add("text", BindingSourcescrlDetailAdd, "mark");
                BindingSourcescrlDetailAdd.AddNew();
                textbillNo.Text = billNo;
            }

         
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textqty.Text == "")
            {
                MessageBox.Show("请输入数量");
               
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        BindingSourcescrlDetailAdd.EndEdit(); //编辑到完成，写到数据源
                        BindingSourcescrlDetailAdd.CancelEdit(); //刷新数据作用

                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            BindingSourcescrlDetailAdd.CancelEdit();
            this.Close();
        }
        //选择原材料
        private void buttworkerselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "matNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.materialList , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textmatNum.Text  = productselect.pname;
            }
        }
       
        //原材料值发生改变之后
        private void textmatNum_TextChanged(object sender, EventArgs e)
        {
            try
            {
                textunit.Text = basicSql.getunitbymaterialList(textmatNum.Text);
                textmatSpec.Text = basicSql.getmatSpecbymaterialList(textmatNum.Text);
                textmatName.Text = basicSql.getmatNamebymaterialList (textmatNum.Text);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textmatName_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}
